<?php

/**
 * Created by PhpStorm.
 * User: DD
 * Date: 2016/4/17
 * Time: 22:22
 */
namespace app\admin\controller;

use app\admin\services\MenuService;
use app\common\model\Admin;
use app\common\model\Game;
use app\common\model\Menu;
use app\common\model\RoleMenu;
use app\common\services\LoginService;
use app\common\util\MenuManager;
use app\common\util\ValueBag;
use think\Config;
use think\Controller;
use app\common\controller\Base;

class Index extends Base
{
    public function index()
    {
        die;
    }

    public function site()
    {
        $menuService = new MenuService();
        $adminRow = Admin::instance()->where(['id' => LoginService::getLoginUserId()])->field('avatar,role_ids')->find();
        $bag = ['menuList' => $menuService->getMenuList(LoginService::isSuperMan() ? false : true, $adminRow['role_ids'])];
        $this->assign('loginName', LoginService::getLoginUserName());
        $this->assign('roleName', LoginService::isSuperMan() ? '超级管理员' : '普通管理员');
        $this->assign('avatar', $adminRow['avatar']);
        $this->assign('bag', $bag);
        $this->assign('config', json_encode(['OSS_IMAGE_HOST' => Config::get('oss.OSS_IMAGE_HOST')]));
        return $this->fetch('index');
    }

    public function desktop()
    {
        $this->assign('m', ['member_total' => \app\common\model\Member::instance()->count()]);
        return $this->fetch();
    }

    public function test()
    {
        return $this->fetch();
    }

    public function setSiteId()
    {
        session('site_id', I('get.id', 0, 'intval'));
    }

    public function site2()
    {
        return $this->fetch();
    }

    public function options()
    {
        if (LoginService::isSuperMan()) {
            $js = 'var _needCheckPower=0;';
        } else {
            $adminRow = Admin::instance()->where(['id' => LoginService::getLoginUserId()])->field('role_ids')->find();
            $role_ids = $adminRow['role_ids'];
            $js = 'var _needCheckPower=1;';
            if (!empty($role_ids)) {
                $roleMenuRows = RoleMenu::instance()->where([
                    'role_id' => ['in', explode(',', $role_ids)]
                ])->select();
                $tokens = [];
                foreach ($roleMenuRows as $menuRow) {
                    $tokens[] = $menuRow['menu_token'];
                }
                $mactions = MenuManager::getActions($tokens);
                if (!empty($mactions)) {
                    $js .= 'var _actionPowerMap=' . json_encode($mactions, true);
                }
            }
        }
        return 'var YOptions=' . json_encode(ValueBag::getOptions()) . ';' . $js . ';var YConfig={picDomain:""}';
    }
}